package com.yenlien.traceyun.plant.dao;

import com.alibaba.fastjson.JSONObject;
import com.yenlien.traceyun.plant.entity.SDRKData;
import com.yenlien.traceyun.webase.dao.BaseJpaDao;
import org.springframework.data.jpa.repository.Query;

import java.util.List;


public interface SDRKDataDao extends BaseJpaDao<SDRKData> {



    @Query(value = "SELECT param_name paramName, MAX(param_val) max, MIN(param_val) min FROM t_sdrk WHERE c_time BETWEEN CONCAT(DATE(NOW()), ' 00:00:00') AND CONCAT(DATE(NOW()), ' 23:59:59') " +
            "GROUP BY param_name", nativeQuery = true)
    List<JSONObject> getDay();

    @Query(value = "SELECT param_name paramName, MAX(param_val) max, MIN(param_val) min FROM t_sdrk WHERE c_time BETWEEN DATE_FORMAT(NOW() - INTERVAL (WEEKDAY(NOW())) DAY, '%Y-%m-%d')" +
            " AND DATE_FORMAT(NOW() + INTERVAL (6 - WEEKDAY(NOW())) DAY, '%Y-%m-%d') GROUP BY param_name", nativeQuery = true)
    List<JSONObject> getWeek();

    @Query(value = "SELECT param_name paramName, MAX(param_val) max, MIN(param_val) min FROM t_sdrk WHERE c_time BETWEEN  CONCAT(DATE_FORMAT(NOW(), '%Y-%m'), '-01 00:00:00')" +
            " AND CONCAT(LAST_DAY(NOW()), ' 23:59:59') GROUP BY param_name", nativeQuery = true)
    List<JSONObject> getMonth();



    @Query(value = "SELECT param_name paramName, ANY_VALUE(param_unit) paramUnit FROM t_sdrk WHERE equipment_id = ?1 GROUP BY param_name", nativeQuery = true)
    List<JSONObject> getParamNameList(String equId);


    @Query(value = "SELECT * FROM t_sdrk WHERE equipment_id = ?1 ORDER BY c_time DESC LIMIT ?2", nativeQuery = true)
    List<SDRKData> getLast(String equId, Integer limit);


    @Query(value = "SELECT * FROM t_sdrk WHERE equipment_id = ?1 AND c_time BETWEEN ?2 AND ?3", nativeQuery = true)
    List<SDRKData> getHistoryData(String equId, String startTime, String endTime);






}
